package descentFly.away3D4.Sprite
{
	import away3d.cameras.Camera3D;
	import away3d.entities.Sprite3D;
	import away3d.materials.BitmapMaterial;
	import away3d.materials.MaterialBase;
	
	import descentFly.away3D4.df_timer;
	import descentFly.df_utils;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.geom.Matrix;
	import flash.geom.Matrix3D;
	import flash.geom.Point;
	import flash.geom.Vector3D;
	import flash.utils.Timer;
	
	public class df_animSprite extends Sprite3D {
		private var _bitmapData		: BitmapData;
		private var _frames			: Array;
		private var _timer			: Timer;
		private var _completeFunc	: Function;
		
		public function df_animSprite(width:Number, height:Number, a_frames: Array) {
			_bitmapData = new BitmapData(width, height, true, 0x00000000);
			var mat: BitmapMaterial = new BitmapMaterial(_bitmapData);
			mat.alphaBlending = true;
			super(mat, width, height);
			_frames = a_frames;
		}
		
		public function begin(a_completeFunc: Function=null): void {
			if (!_timer) {
				_timer = new Timer(df_timer.AMINSPRITE_DELAY, (a_completeFunc != null)?(_frames.length + 1):0);
				_timer.addEventListener(TimerEvent.TIMER, doTimer);
				_timer.start();
				_completeFunc = a_completeFunc;
			}
		}
		
		public function stop(): void {
			if (_timer) {
				_timer.stop();
				_timer.removeEventListener(TimerEvent.TIMER, doTimer);
				_timer = null;
			}
		}
		
		public function get BmpMat(): BitmapMaterial {
			return material as BitmapMaterial;
		}
		
		protected function doTimerComplete(): void {
			BmpMat.bitmapData.fillRect(BmpMat.bitmapData.rect, 0x00000000);
			BmpMat.updateTexture();
			stop();
			_completeFunc();
		}
		
/*		
		override public function pushModelViewProjection(camera: Camera3D) : void {
			var comps : Vector.<Vector3D>;
			var rot : Vector3D;
			if (++_mvpIndex == _stackLen) {
				_mvpTransformStack[_mvpIndex] = new Matrix3D();
				++_stackLen;
			}
			
			// todo: find better way
			var mvp : Matrix3D = _mvpTransformStack[_mvpIndex];
			mvp.copyFrom(sceneTransform);
			mvp.append(camera.inverseSceneTransform);
			
			comps = mvp.decompose();
			rot = comps[1];
			rot.x = rot.y = rot.z = 0;
			mvp.recompose(comps);
			
			mvp.append(camera.lens.matrix);
			mvp.copyColumnTo(3, _pos);
			_zIndices[_mvpIndex] = -_pos.z;
			
		}
*/		
		
		protected function doTimer(e: Event): void {
			if ((_completeFunc != null) && 
				(_timer.currentCount > _frames.length)) doTimerComplete();
			else {
				var frame: Bitmap = _frames[(_timer.currentCount - 1) % _frames.length];
				var mat: Matrix = new Matrix();
				mat.scale(BmpMat.bitmapData.width / frame.bitmapData.width,
					BmpMat.bitmapData.height / frame.bitmapData.height);
				
				BmpMat.bitmapData.fillRect(BmpMat.bitmapData.rect, 0x00000000);
				BmpMat.bitmapData.draw(frame, mat, null, null, null, true);
				BmpMat.updateTexture();
			}
		}
	}
}